package mercurius.fitness.client.rb.diet;

import java.util.HashMap;
import java.util.Vector;

import mercurius.fitness.client.util.MercuriusHelper;
import mercurius.fitness.client.util.AbstractResponseCommand;
import net.pleso.framework.client.dal.SelectParams;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class DietDataSet {
	
	public DietDataSet() {

	}

	public void select(final SelectParams params, final AsyncCallback callback,final String type) {
		
		AbstractResponseCommand pass = new AbstractResponseCommand() {
			
			   public void execute() {
				   
			   }

			public Object getObject() {
				return params;
			}

			public void setObject(Object obj) {
				Vector props = (Vector) obj;
				Diet[] diets = new Diet[props.size()];
				for(int i=0;i<props.size();i++) {
					HashMap diet = (HashMap)props.get(i);
					diets[i] = new Diet(diet);
					diets[i].setType(type);
				}
				//	Arrays.sort(diets, new DataRowComparator(params.getOrderByColumnName(), params.getOrderByDirection()));
					callback.onSuccess(diets);
			}
			 };
			
			Diet d = new Diet();
			d.setType(type);
			MercuriusHelper.updateMercurius(d,MercuriusHelper.MERCURIUS_SELECT,callback,pass);
	}
	
	public void selectCount(AsyncCallback callback,String type) {
		Diet d = new Diet();
		d.setType(type);
		MercuriusHelper.updateMercurius(d,MercuriusHelper.MERCURIUS_SELECT_COUNT,callback);
	}
	
	public void add(final Diet diet, AsyncCallback callback,String type) {
		diet.setType(type);
			AbstractResponseCommand pass = new AbstractResponseCommand() {
			   public void execute() {
				   diet.setID(new Integer(results));
			   }

			public Object getObject() {
				return null;
			}

			public void setObject(Object obj) {
				
			}
			 };
			
			MercuriusHelper.updateMercurius(diet,MercuriusHelper.MERCURIUS_INSERT,callback,pass);
	}
	
	public void update(final Diet diet, AsyncCallback callback,String type) {
		diet.setType(type);
		MercuriusHelper.updateMercurius(diet,MercuriusHelper.MERCURIUS_UPDATE,callback);
	}
	
	public void get(final Integer id, final AsyncCallback callback,final String type)
	{
		
		AbstractResponseCommand pass = new AbstractResponseCommand() {
			
			   public void execute() {
				   
			   }

			public Object getObject() {
				return id;
			}

			public void setObject(Object obj) {
				
				
				HashMap m = (HashMap) obj;
				Diet diet = new Diet(m);
				diet.setType(type);
				callback.onSuccess(diet);
			}
			 };
			 
			Diet d = new Diet();
			d.setType(type);
			MercuriusHelper.updateMercurius(d,MercuriusHelper.MERCURIUS_SELECT_BY_ID,callback,pass);

	}
	
	public void delete(Integer id, AsyncCallback callback,String type)
	{
		Diet diet = new Diet();
		diet.setType(type);
		diet.setID(id);
		MercuriusHelper.updateMercurius(diet,MercuriusHelper.MERCURIUS_DELETE,callback);
	}
}
